/**
 * Define a namespace for the application.
 */
window.app = {};
var app = window.app;


/**
 * @constructor
 * @extends {ol.interaction.Pointer}
 */
app.Drag = function (map) {

    ol.interaction.Pointer.call(this, {
        handleDownEvent: app.Drag.prototype.handleDownEvent,
        handleDragEvent: app.Drag.prototype.handleDragEvent,
        handleMoveEvent: app.Drag.prototype.handleMoveEvent,
        handleUpEvent: app.Drag.prototype.handleUpEvent
    });

    /**
     * @type {ol.Pixel}
     * @private
     */
    this.coordinate_ = null;

    /**
     * @type {string|undefined}
     * @private
     */
    this.cursor_ = 'pointer';

    /**
     * @type {ol.Feature}
     * @private
     */
    this.feature_ = null;
    this._map=map
    /**
     * @type {string|undefined}
     * @private
     */
    this.previousCursor_ = undefined;

};
ol.inherits(app.Drag, ol.interaction.Pointer);


var point_flag = 'point_flag_';

var arrow = 'arrow';
var region = 'region';
var first = 'first';
var second = 'second';
var third = 'third';
/**
 * @param {ol.MapBrowserEvent} evt Map browser event.
 * @return {boolean} `true` to start the drag sequence.
 */
app.Drag.prototype.handleDownEvent = function (evt) {
    var map = evt.map;

    var feature = map.forEachFeatureAtPixel(evt.pixel,
        function (feature, layer) {
            return feature;
        });

    if (feature) {
        var id = feature.I.id + '';
        // if (id.indexOf(arrow) >= 0 || id.indexOf(region) >= 0) {
        //     var aid = id.substr(arrow.length);
        //     if (id.indexOf(arrow) >= 0) {
        //         aid = id.substr(arrow.length);
        //     }
        //     if (id.indexOf(region) >= 0) {
        //         aid = id.substr(region.length);
        //     }

        //     // $.operate.submit1(ctx + "system/tab-vector-do/getById/" + aid, "get", "json", "", function (data) {
        //     //     source_new_point.clear();
        //     //     var p = ol.proj.transform([data.p10, data.p11], proj, proj_m);
        //     //     var p1 = ol.proj.transform([data.p20, data.p21], proj, proj_m);
        //     //     var p2 = ol.proj.transform([data.p30, data.p31], proj, proj_m);
        //     //     var style = createStyle(1, 0.2, false, 0, "/img/point.png", 'center', 'bottom', 'bold 12px 幼圆', '', '#aa3300');
        //     //     var po = createPoint(point_flag + (first + aid), point_flag, p[0], p[1], style);
        //     //     source_new_point.addFeature(po);

        //     //     var po1 = createPoint(point_flag + (second + aid), point_flag, p1[0], p1[1], style);
        //     //     source_new_point.addFeature(po1);

        //     //     var po2 = createPoint(point_flag + (third + aid), point_flag, p2[0], p2[1], style);
        //     //     source_new_point.addFeature(po2);
        //     //     // popup.setPosition(p);
        //     //     // content.innerHTML = createHtml(feature, data, 2);
        //     // });
        //     return;
        // }
        this.coordinate_ = evt.coordinate;
        this.feature_ = feature;
    }

    return !!feature;
};


/**
 * @param {ol.MapBrowserEvent} evt Map browser event.
 */
app.Drag.prototype.handleDragEvent = function (evt) {
    var map = evt.map;

    var feature = map.forEachFeatureAtPixel(evt.pixel,
        function (feature, layer) {
            return feature;
        });

    var deltaX = evt.coordinate[0] - this.coordinate_[0];
    var deltaY = evt.coordinate[1] - this.coordinate_[1];

    var geometry = /** @type {ol.geom.SimpleGeometry} */
        (this.feature_.getGeometry());
    geometry.translate(deltaX, deltaY);

    this.coordinate_[0] = evt.coordinate[0];
    this.coordinate_[1] = evt.coordinate[1];
};


/**
 * @param {ol.MapBrowserEvent} evt Event.
 */
app.Drag.prototype.handleMoveEvent = function (evt) {
    if (this.cursor_) {
        var map = evt.map;
        var feature = map.forEachFeatureAtPixel(evt.pixel,
            function (feature, layer) {
                return feature;
            });
        var element = evt.map.getTargetElement();
        if (feature) {
            if (element.style.cursor != this.cursor_) {
                this.previousCursor_ = element.style.cursor;
                element.style.cursor = this.cursor_;
            }
        } else if (this.previousCursor_ !== undefined) {
            element.style.cursor = this.previousCursor_;
            this.previousCursor_ = undefined;
        }
    }
};


/**
 * @param {ol.MapBrowserEvent} evt Map browser event.
 * @return {boolean} `false` to stop the drag sequence.
 */
app.Drag.prototype.handleUpEvent = function (evt) {
    console.log(this._map);
    var feature = this._map.forEachFeatureAtPixel(evt.pixel,
        function (feature, layer) {
            var id = feature.I.id + '';
            if (id != undefined) {
                var type = 0;
               if (id.indexOf(point_flag) >= 0) {
                    var aid = id.substr(point_flag.length);
                    var att;
                    // var p = ol.proj.transform(evt.coordinate, proj_m, proj);
                    console.log(evt.coordinate);
                    if (aid.indexOf(first) >= 0) {
                        aid = aid.substr(first.length);
                        att = [evt.coordinate, [0, 0], [0, 0]]
                    } else if (aid.indexOf(second) >= 0) {
                        aid = aid.substr(second.length);
                        att = [[0, 0], evt.coordinate, [0, 0]]
                    } else if (aid.indexOf(third) >= 0) {
                        aid = aid.substr(third.length);
                        att = [[0, 0], [0, 0], evt.coordinate]
                    } else {
                        return;
                    }
                } else {
                    var p = ol.proj.transform(evt.coordinate, proj_m, proj);
                    // var data = "&id=" + feature.I.id + "&jd=" + p[0] + "&wd=" + p[1];
                    // $.operate.submit1(ctx + "system/tab-dw-do/edit", "post", "json", data, function (data) {
                    // });
                }
            }
            return feature;
        });

    this.coordinate_ = null;
    this.feature_ = null;
    return false;
};

module.exports = {
    app,
}